# $Id: MT2Blogger.pm 4 2009-04-28 03:30:37Z hirotaka.ogawa $

package MT::Tool::MT2Blogger;
use strict;
use warnings;

use base qw( MT::Tool );

our $VERSION = '0.1';

sub usage {
'--postURI=http://www.blogger.com/feeds/<blogID>/posts/default --username=<username> --password=<password>';
}

sub help {
    return qq{
mt2blogger $VERSION - A CLI for converting from MT to Google Blogger

    --postURI=http://www.blogger.com/feeds/<blogID>/posts/default

        Specify the Blogger post URL.  This URL will be found as the
        URL in the <link rel="service.post"> tag that appears in the
        <head> section of the human-readable version of the Blogger
        blog.

    --username=<username>

        Specify the Blogger user's name.

    --password=<password>

        Specify the Blogger user's password.

    --blog_id=<id>

        Specify the ID of the MT blog.

};
}

my ( $postURI, $username, $password, $blog_id );

sub options {
    return (
        'postURI=s'  => \$postURI,
        'username=s' => \$username,
        'password=s' => \$password,
        'blog_id=i'  => \$blog_id,
    );
}

sub main {
    my $class = shift;
    my ($verbose) = $class->SUPER::main(@_);
    unless ( $postURI && $username && $password && $blog_id ) {
        $class->show_help();
        exit;
    }

    require MT::Tool::MT2Blogger::GDataClient;
    my $api = MT::Tool::MT2Blogger::GDataClient->new;
    $api->username($username);
    $api->password($password);
    $api->service('blogger');

    require MT::Entry;
    my $iter = MT::Entry->load_iter(
        {
            blog_id => $blog_id,
            status  => MT::Entry::RELEASE(),
        },
        {
            sort      => 'authored_on',
            direction => 'ascend',
        }
    );

    while ( my $entry = $iter->() ) {
        next if $entry->keywords;
        my $gentry = mt_to_gdata($entry);
        my $editURI = $api->createEntry( $postURI, $gentry )
          or die $api->errstr;
        $entry->keywords($editURI);
        $entry->save or die $entry->errstr;

        my $created = $api->getEntry($editURI);
        my @links   = $created->link;
        my @replies =
          grep { $_->rel eq 'replies' && $_->type eq 'application/atom+xml' }
          @links;
        my @alternate =
          grep { $_->rel eq 'alternate' && $_->type eq 'text/html' } @links;
        print "MT       Permalink: " . $entry->permalink . "\n";
        print "Blogger  Permalink: " . $alternate[0]->href . "\n";
        print "Blogger CommentURI: " . $replies[0]->href . "\n\n";
    }
    1;
}

sub mt_to_gdata {
    my $entry = shift;

    require MT::Template::Context;
    my $ctx = MT::Template::Context->new;
    $ctx->stash( 'entry', $entry );

    require XML::Atom::Entry;
    my $gentry = XML::Atom::Entry->new;
    require MT::Util;
    $gentry->title( MT::Util::remove_html( $entry->title ) );
    $gentry->content( MT::Template::Context::_hdlr_entry_body($ctx)
          . MT::Template::Context::_hdlr_entry_more($ctx) );

    $gentry->published(
        MT::Template::Context::_hdlr_entry_date(
            $ctx,
            {
                utc    => 1,
                format => '%Y-%m-%dT%H:%M:%SZ',
            }
        )
    );
    $gentry->updated(
        MT::Template::Context::_hdlr_entry_mod_date(
            $ctx,
            {
                utc    => 1,
                format => '%Y-%m-%dT%H:%M:%SZ',
            }
        )
    );

#    if ($entry->status eq MT::Entry::HOLD()) {
#        require XML::Atom::Util;
#        my $app = XML::Atom::Namespace->new(app => 'http://www.w3.org/2007/app');
#        my $draft = XML::Atom::Util::create_element($app, 'draft');
#    }

    require XML::Atom::Person;
    my $author  = $entry->author;
    my $gauthor = XML::Atom::Person->new;
    $gauthor->name( $author->name );
    $gauthor->email( $author->email ) if $author->email;
    $gauthor->uri( $author->url )     if $author->url;
    $gentry->author($gauthor);

    require XML::Atom::Category;
    my $gcat = XML::Atom::Category->new;
    $gcat->scheme('http://schemas.google.com/g/2005#kind');
    $gcat->term('http://schemas.google.com/blogger/2008/kind#post');
    $gentry->add_category($gcat);

    my $cats = $entry->categories;
    for my $cat (@$cats) {
        my $gcat = XML::Atom::Category->new;
        $gcat->scheme('http://www.blogger.com/atom/ns#');
        $gcat->term( $cat->label );
        $gentry->add_category($gcat);
    }
    my @tags = $entry->tags;
    for my $tag (@tags) {
        my $gcat = XML::Atom::Category->new;
        $gcat->scheme('http://www.blogger.com/atom/ns#');
        $gcat->term($tag);
        $gentry->add_category($gcat);
    }

    $gentry;
}

1;
__END__
